#ifndef CONVERTED_H
#define CONVERTED_H
#include "token.h"
#include "buffer.h"
typedef struct{
	token tok;
	int p;
}CODE;
typedef struct{
	int moda;
	int a;
	int modb;
	int b;
	int nextp;
}WHILEVALUE;
CODE code[65536];
WHILEVALUE whileValue[65536];
typedef char string[33];
string tempcnts[1<<10];
int value[1<<10];
unsigned cntstion=1;
int lookup(string s)
{
	int i;
	for(i=1;i<cntstion;i++)
		if(strcmp(s,tempcnts[i])==0)
			return i;
	return 0;
}
int enter(string s)
{
	if(cntstion==1024)
	{
		post_error(range,'-',0);
		return 0;
	}
	strcpy_s(tempcnts[cntstion++], 33, s);
	return cntstion-1;
}
int whileTot=1;
int codeTot=1;
int addCode(token tok,int p)
{
	if(codeTot==65535)
	{
		post_error(range,codeTot,0);
		printf("Code limit 65535\n");
		return 0;
	}
	else
	{
		code[codeTot].tok=tok;
		code[codeTot].p=p;
		return codeTot++;
	}
}
int addWhile(int moda,int a,int modb,int b)
{
	if(whileTot==65535)
	{
		post_error(range,codeTot,0);
		printf("While limit 65535\n");
		return 0;
	}
	whileValue[whileTot].moda=moda;
	whileValue[whileTot].a=a;
	whileValue[whileTot].modb=modb;
	whileValue[whileTot].b=b;
	return whileTot++;
}
void execute()
{
	int p=0;
	while(code[p].tok!=HALT)
	{
		switch(code[p].tok)
		{
			case CLEAR:
				value[code[p].p]=0x00;
				break;
			case INCR:
				if(value[code[p].p]<2147483647)
					value[code[p].p]++;
				break;
			case DECR:
				if(value[code[p].p]>0)
					value[code[p].p]--;
				break;
			case WHILE:
				int temp1,temp2,temp4;
				temp1=code[p].p;
				temp2=whileValue[temp1].a;
				temp4=whileValue[temp1].b;
				if(whileValue[temp1].moda==VALUE)
					temp2=value[temp2];
				if(whileValue[temp1].modb==VALUE)
					temp4=value[temp4];
				if(temp2==temp4)
					p=whileValue[temp1].nextp;
				break;
			case NEXT:
				p=code[p].p-1;
				break;
		}
		p++;
	}
}
#endif
